<template>
	<view>
		<u-tabbar
			:value="currentMenu"
			@change="clickBottomMenu"
			activeColor="#000"
			:fixed="true"	
			:placeholder="true"
			:safeAreaInsetBottom="true"
		>		
			<u-tabbar-item text="发现">
				<image
					class="bottom_img"
					slot="active-icon"
					src="/static/01.png"
				></image>
				<image
					class="bottom_img"
					slot="inactive-icon"
					src="/static/01_normal.png"
				></image>
			</u-tabbar-item>
			
			<u-tabbar-item text="商户">
				<image
					class="bottom_img"
					slot="active-icon"
					src="/static/06.png"
				></image>
				<image
					class="bottom_img"
					slot="inactive-icon"
					src="/static/06_normal.png"
				></image>
			</u-tabbar-item>
			
			<u-tabbar-item text=" ">
				<image
					class="bottom_img_mid"
					slot="active-icon"
					src="/static/03_normal.png"
				></image>
				<image
					class="bottom_img_mid"
					slot="inactive-icon"
					src="/static/03_normal_2.png"
				></image>
			</u-tabbar-item>
			
			<u-tabbar-item text="消息" :badge="notifications_count">
				<image
					class="bottom_img"
					slot="active-icon"
					src="/static/message.png"
				></image>
				<image
					class="bottom_img"
					slot="inactive-icon"
					src="/static/message_normal.png"
				></image>
			</u-tabbar-item>
			
			<u-tabbar-item text="我的">
				<image
					class="bottom_img"
					slot="active-icon"
					src="/static/05.png"
				></image>
				<image
					class="bottom_img"
					slot="inactive-icon"
					src="/static/05_normal.png"
				></image>
			</u-tabbar-item>
		</u-tabbar>		
	</view>
</template>

<script>	
	
	
	export default {
		name:"bottomMenu",
		data() {
			return {
				currentMenu : '',
				notifications_count : '',				
				onlineCheck : false
			};
		},
		mounted:function(){			
			let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
			let curRoute = routes[routes.length - 1].route // 获取当前页面路由，也就是最后一个打开的页面路由
			if( curRoute == "pages/index/index") {
				this.currentMenu = 0 
			}
			if( curRoute == "packageShop/merchant/merchantList") {
				this.currentMenu = 1
			}
			if( curRoute == "pages/member/postNew") {
				this.currentMenu = 2
			}
			if( curRoute == "pages/message/message") {
				this.currentMenu = 3
			}
			if( curRoute == "pages/member/member") {
				this.currentMenu = 4 
			}
			// console.log('currentMenu', this.currentMenu)			
			// console.log('buttom_uni.getStorageSync(\'domain\')',uni.getStorageSync('domain'))
			this.load_notifications_count()
			this.online_check()
		},
		methods: {			
			online_check() {
				uni.request({
					url: uni.getStorageSync('domain') + '/api/v1/system/online/check',
					method: 'get',
					header: {
						'Accept': 'application/json',
					},
					success: (res) => {
						this.onlineCheck = res.data.status
						// console.log('online_check', this.onlineCheck)
						uni.hideLoading();
					},
					fail: (res) => {}
				})
			},
			subCount() {
				// console.log('1111111');
				this.notifications_count = this.notifications_count - 1
			},
			load_notifications_count() {				
				uni.showLoading({
					title:"加载中...."
				})
				uni.request({
				url:uni.getStorageSync('domain') + '/api/v1/notifications/stats',					
					method: 'GET',
					header: {
						'Accept': 'application/json',
						'Authorization' : 'Bearer ' + uni.getStorageSync('access_member_info').access_token
					},
					success: res => {
						// console.log('1111111111111',res);
						this.notifications_count = res.data.totle_unread_count
						uni.hideLoading();
					}
				});
			},
			clickBottomMenu(e) {
			    if(e === 0) {
					// console.log('bottomMenu0', e);
					uni.redirectTo({
						url:"/pages/index/index"
					});
				}
			    if(e === 1) {
					// console.log('bottomMenu0', e);
					uni.redirectTo({
						url:"/packageShop/merchant/merchantList"
					});
				}
			    if(e === 2) {
					// console.log('bottomMenu1', e);
					if(this.onlineCheck) {
						uni.redirectTo({
							url:"/pages/member/postType"		
						});
					} else {
						if(uni.getStorageSync("access_member_info"))
						{						
							uni.navigateTo({
								url:"/pages/member/postNew"						
							});					
						} else {
							uni.navigateTo({
								url:"/pages/register/register"
							});	
						}
					}
				}
			    if(e === 3) {
					uni.redirectTo({
						url:"/pages/message/message"						
					});	
				}
			    if(e === 4) {
					uni.redirectTo({
						url:"/pages/member/member"
					});	
				}
			},
		}
	}
</script>

<style lang="scss">
	.bottom_img_mid {
		width: 35px;
		height: 35px;
	}
	.bottom_img {
		width: 23px;
		height: 23px;
	}
</style>